package ckw.gui;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DateTimeSelectorWindow extends Dialog {

	protected Date result;
	protected Shell shell;
	
	private DateTime calendarDtTm;
	private DateTime timeDtTm;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public DateTimeSelectorWindow(Shell parent, Date date) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		createContents();
		if(date != null) {
			result = date;
			setContentsValue(date);
		}
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Date open() {
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		setText("Date & time selector");
		
		shell = new Shell(getParent(), getStyle());
		shell.setSize(240, 320);
		shell.setText(getText());
		shell.setLayout(new GridLayout(1, false));
		
		calendarDtTm = new DateTime(shell, SWT.BORDER | SWT.CALENDAR);
		calendarDtTm.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 2));
		
		timeDtTm = new DateTime(shell, SWT.BORDER | SWT.TIME);
		timeDtTm.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		
		Composite cmpButtons = new Composite(shell, SWT.NONE);
		cmpButtons.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cmpButtons.setSize(215, 40);
		cmpButtons.setLayout(new GridLayout(2, false));
		
		Button btnOK = new Button(cmpButtons, SWT.NONE);
		btnOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				updateResult();
				shell.close();
			}
		});
		GridData gd_btnOK = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_btnOK.widthHint = 100;
		btnOK.setLayoutData(gd_btnOK);
		btnOK.setBounds(0, 0, 90, 30);
		btnOK.setText("OK");
		
		Button btnCancel = new Button(cmpButtons, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.close();
			}
		});
		GridData gd_btnCancel = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_btnCancel.widthHint = 100;
		btnCancel.setLayoutData(gd_btnCancel);
		btnCancel.setBounds(0, 0, 90, 30);
		btnCancel.setText("Cancel");
	}
	
	private void setContentsValue(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendarDtTm.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		timeDtTm.setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
	}
	
	private void updateResult() {
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(Calendar.YEAR, calendarDtTm.getYear());
		calendar.set(Calendar.MONTH, calendarDtTm.getMonth());
		calendar.set(Calendar.DAY_OF_MONTH, calendarDtTm.getDay());
		calendar.set(Calendar.HOUR_OF_DAY, timeDtTm.getHours());
		calendar.set(Calendar.MINUTE, timeDtTm.getMinutes());
		calendar.set(Calendar.SECOND, timeDtTm.getSeconds());
		
		result = new Date(calendar.getTimeInMillis());
	}
}
